/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Temporal;

/**
 * Diese Klasse stellt eine Version eines bestimmten Beitrags da welcher eine menge an Posts
 * beinhaltet.
 * 
 * @author Patrick Kuhlmann
 */
@RequestScoped
@Entity
public class Version implements Serializable{
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    //@ManyToOne
    //private Article article;
    private int versionNumber;
    private boolean Visible;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date date;
    @ManyToMany(cascade=CascadeType.ALL)
    private List<Post> posts;

    public Version(int versionNumber, boolean Visible, Date date, List<Post> posts) {
        this.versionNumber = versionNumber;
        this.Visible = Visible;
        this.date = date;
        this.posts = posts;
    }

    public Version() {
    }
    
    public void addPost(Post p){
        if(posts!=null){
            posts.add(p);
        }else{
            posts=new ArrayList<>();
            
            posts.add(p);
        }
        
    }
    
     public Post getFirstPosts() {
        if(!posts.isEmpty()){
         return posts.get(0);
        }else{
            return null;
        }
       
    }
    
    public List<Post> getPosts() {
        return posts;
    }

    public void setPosts(List<Post> posts) {
        this.posts = posts;
    }
  
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getVersionNumber() {
        return versionNumber;
    }

    public void setVersionNumber(int versionNumber) {
        this.versionNumber = versionNumber;
    }

    public boolean isVisible() {
        return Visible;
    }

    public void setVisible(boolean Visible) {
        this.Visible = Visible;
    }

    public String getDate() {
        DateFormat formatter = new SimpleDateFormat();
        return formatter.format(date);
    }

    public void setDate(Date date) {
        this.date = date;
    }
    
    
}
